﻿//==================================================================
// 文件名: V_BH_AccountsReceivable.cs
// 作 者：代码自动生成
// 日 期：2015/4/1 21:30:19
// 描 述：
// 版 本：1.00
// 修改历史纪录
// 版 本  修改时间      修改人            修改内容
// 1.00             
//==================================================================
using System;
using System.Collections.Generic;
using System.Linq;
using System.ComponentModel;
using System.Text;
using Csla;
using lite;
using Csla.Validation;
using Csla.Data;
using System.Data.SqlClient;
using ZX.DAL;
using OpenExpressApp.ORM;

namespace DCBH.Library
{
    /// <summary>
    /// 
    /// </summary>
    [Serializable]
    [Table(Name = "V_BH_AccountsReceivable")]
    [ZX.DBModule.Table("V_BH_AccountsReceivable", "ID")]
    public class V_BH_AccountsReceivable : ZX.DBModule.BaseEntity
    {
        #region Business Methods 业务方法
        /// <summary>
        /// 
        /// </summary>
        private static PropertyInfo<Guid> IDProperty = RegisterProperty(typeof(V_BH_AccountsReceivable), new PropertyInfo<Guid>("ID"));
        [DataObjectField(true, true)]
        [Column, PK]
        public Guid ID
        {
            get { return GetProperty(IDProperty); }
            set { SetProperty(IDProperty, value); }
        }

        /// <summary>
        /// 
        /// </summary>
        private static PropertyInfo<string> SystemGenNoProperty = RegisterProperty(typeof(V_BH_AccountsReceivable), new PropertyInfo<string>("SystemGenNo"));
        [Column]
        public string SystemGenNo
        {
            get { return GetProperty(SystemGenNoProperty); }
            set { SetProperty(SystemGenNoProperty, value); }
        }
        /// <summary>
        /// 
        /// </summary>
        private static PropertyInfo<string> AccountsReceivableNoProperty = RegisterProperty(typeof(V_BH_AccountsReceivable), new PropertyInfo<string>("AccountsReceivableNo"));
        [Column]
        public string AccountsReceivableNo
        {
            get { return GetProperty(AccountsReceivableNoProperty); }
            set { SetProperty(AccountsReceivableNoProperty, value); }
        }
        /// <summary>
        /// 
        /// </summary>
        private static PropertyInfo<DateTime> SummaryDateProperty = RegisterProperty(typeof(V_BH_AccountsReceivable), new PropertyInfo<DateTime>("SummaryDate"));
        [Column]
        public DateTime SummaryDate
        {
            get { return GetProperty(SummaryDateProperty); }
            set { SetProperty(SummaryDateProperty, value); }
        }
        /// <summary>
        /// 
        /// </summary>
        private static PropertyInfo<DateTime> SumStartDateProperty = RegisterProperty(typeof(V_BH_AccountsReceivable), new PropertyInfo<DateTime>("SumStartDate"));
        [Column]
        public DateTime SumStartDate
        {
            get { return GetProperty(SumStartDateProperty); }
            set { SetProperty(SumStartDateProperty, value); }
        }
        /// <summary>
        /// 
        /// </summary>
        private static PropertyInfo<DateTime> SumEndDateProperty = RegisterProperty(typeof(V_BH_AccountsReceivable), new PropertyInfo<DateTime>("SumEndDate"));
        [Column]
        public DateTime SumEndDate
        {
            get { return GetProperty(SumEndDateProperty); }
            set { SetProperty(SumEndDateProperty, value); }
        }
        /// <summary>
        /// 
        /// </summary>
        private static PropertyInfo<string> CustomerNoProperty = RegisterProperty(typeof(V_BH_AccountsReceivable), new PropertyInfo<string>("CustomerNo"));
        [Column]
        public string CustomerNo
        {
            get { return GetProperty(CustomerNoProperty); }
            set { SetProperty(CustomerNoProperty, value); }
        }
        /// <summary>
        /// 
        /// </summary>
        private static PropertyInfo<string> CustomerNameProperty = RegisterProperty(typeof(V_BH_AccountsReceivable), new PropertyInfo<string>("CustomerName"));
        [Column]
        public string CustomerName
        {
            get { return GetProperty(CustomerNameProperty); }
            set { SetProperty(CustomerNameProperty, value); }
        }
        /// <summary>
        /// 
        /// </summary>
        private static PropertyInfo<decimal> AmountProperty = RegisterProperty(typeof(V_BH_AccountsReceivable), new PropertyInfo<decimal>("Amount"));
        [Column]
        public decimal Amount
        {
            get { return GetProperty(AmountProperty); }
            set { SetProperty(AmountProperty, value); }
        }
        /// <summary>
        /// 
        /// </summary>
        private static PropertyInfo<decimal> RebateProperty = RegisterProperty(typeof(V_BH_AccountsReceivable), new PropertyInfo<decimal>("Rebate"));
        [Column]
        public decimal Rebate
        {
            get { return GetProperty(RebateProperty); }
            set { SetProperty(RebateProperty, value); }
        }
        /// <summary>
        /// 
        /// </summary>
        private static PropertyInfo<decimal> RebateAmountProperty = RegisterProperty(typeof(V_BH_AccountsReceivable), new PropertyInfo<decimal>("RebateAmount"));
        [Column]
        public decimal RebateAmount
        {
            get { return GetProperty(RebateAmountProperty); }
            set { SetProperty(RebateAmountProperty, value); }
        }
        /// <summary>
        /// 
        /// </summary>
        private static PropertyInfo<decimal> FactAmountProperty = RegisterProperty(typeof(V_BH_AccountsReceivable), new PropertyInfo<decimal>("FactAmount"));
        [Column]
        public decimal FactAmount
        {
            get { return GetProperty(FactAmountProperty); }
            set { SetProperty(FactAmountProperty, value); }
        }
        /// <summary>
        /// 
        /// </summary>
        private static PropertyInfo<decimal> ReceivedAmountProperty = RegisterProperty(typeof(V_BH_AccountsReceivable), new PropertyInfo<decimal>("ReceivedAmount"));
        [Column]
        public decimal ReceivedAmount
        {
            get { return GetProperty(ReceivedAmountProperty); }
            set { SetProperty(ReceivedAmountProperty, value); }
        }
        /// <summary>
        /// 
        /// </summary>
        private static PropertyInfo<string> RemarkProperty = RegisterProperty(typeof(V_BH_AccountsReceivable), new PropertyInfo<string>("Remark"));
        [Column]
        public string Remark
        {
            get { return GetProperty(RemarkProperty); }
            set { SetProperty(RemarkProperty, value); }
        }
        /// <summary>
        /// 
        /// </summary>
        private static PropertyInfo<bool> InitFlagProperty = RegisterProperty(typeof(V_BH_AccountsReceivable), new PropertyInfo<bool>("InitFlag"));
        [Column]
        public bool InitFlag
        {
            get { return GetProperty(InitFlagProperty); }
            set { SetProperty(InitFlagProperty, value); }
        }
        /// <summary>
        /// 
        /// </summary>
        private static PropertyInfo<string> ContactProperty = RegisterProperty(typeof(V_BH_AccountsReceivable), new PropertyInfo<string>("Contact"));
        [Column]
        public string Contact
        {
            get { return GetProperty(ContactProperty); }
            set { SetProperty(ContactProperty, value); }
        }
        /// <summary>
        /// 
        /// </summary>
        private static PropertyInfo<string> TelProperty = RegisterProperty(typeof(V_BH_AccountsReceivable), new PropertyInfo<string>("Tel"));
        [Column]
        public string Tel
        {
            get { return GetProperty(TelProperty); }
            set { SetProperty(TelProperty, value); }
        }
        /// <summary>
        /// 
        /// </summary>
        private static PropertyInfo<string> MobileProperty = RegisterProperty(typeof(V_BH_AccountsReceivable), new PropertyInfo<string>("Mobile"));
        [Column]
        public string Mobile
        {
            get { return GetProperty(MobileProperty); }
            set { SetProperty(MobileProperty, value); }
        }
        /// <summary>
        /// 
        /// </summary>
        private static PropertyInfo<string> AddressProperty = RegisterProperty(typeof(V_BH_AccountsReceivable), new PropertyInfo<string>("Address"));
        [Column]
        public string Address
        {
            get { return GetProperty(AddressProperty); }
            set { SetProperty(AddressProperty, value); }
        }
        /// <summary>
        /// 
        /// </summary>
        private static PropertyInfo<string> MicroMsgProperty = RegisterProperty(typeof(V_BH_AccountsReceivable), new PropertyInfo<string>("MicroMsg"));
        [Column]
        public string MicroMsg
        {
            get { return GetProperty(MicroMsgProperty); }
            set { SetProperty(MicroMsgProperty, value); }
        }
        /// <summary>
        /// 
        /// </summary>
        private static PropertyInfo<string> QQProperty = RegisterProperty(typeof(V_BH_AccountsReceivable), new PropertyInfo<string>("QQ"));
        [Column]
        public string QQ
        {
            get { return GetProperty(QQProperty); }
            set { SetProperty(QQProperty, value); }
        }
        /// <summary>
        /// 
        /// </summary>
        private static PropertyInfo<decimal> CreditProperty = RegisterProperty(typeof(V_BH_AccountsReceivable), new PropertyInfo<decimal>("Credit"));
        [Column]
        public decimal Credit
        {
            get { return GetProperty(CreditProperty); }
            set { SetProperty(CreditProperty, value); }
        }
        /// <summary>
        /// 
        /// </summary>
        private static PropertyInfo<int> CreditPeriodProperty = RegisterProperty(typeof(V_BH_AccountsReceivable), new PropertyInfo<int>("CreditPeriod"));
        [Column]
        public int CreditPeriod
        {
            get { return GetProperty(CreditPeriodProperty); }
            set { SetProperty(CreditPeriodProperty, value); }
        }
        /// <summary>
        /// 
        /// </summary>
        private static PropertyInfo<Guid> AddUserProperty = RegisterProperty(typeof(V_BH_AccountsReceivable), new PropertyInfo<Guid>("AddUser"));
        [Column]
        public Guid AddUser
        {
            get { return GetProperty(AddUserProperty); }
            set { SetProperty(AddUserProperty, value); }
        }
        /// <summary>
        /// 
        /// </summary>
        private static PropertyInfo<string> AddUserNameProperty = RegisterProperty(typeof(V_BH_AccountsReceivable), new PropertyInfo<string>("AddUserName"));
        [Column]
        public string AddUserName
        {
            get { return GetProperty(AddUserNameProperty); }
            set { SetProperty(AddUserNameProperty, value); }
        }
        /// <summary>
        /// 
        /// </summary>
        private static PropertyInfo<DateTime> AddTimeProperty = RegisterProperty(typeof(V_BH_AccountsReceivable), new PropertyInfo<DateTime>("AddTime"));
        [Column]
        public DateTime AddTime
        {
            get { return GetProperty(AddTimeProperty); }
            set { SetProperty(AddTimeProperty, value); }
        }
        #endregion

        #region Validation Rules 验证规则
        //将验证规则与业务对象的属性联系在一起
        protected override void AddBusinessRules()
        {
            //AddRule的第一个参数是委托变量，也就是说是方法的变量
            ValidationRules.AddRule(CommonRules.StringRequired, "SystemGenNo");
            ValidationRules.AddRule(CommonRules.StringMaxLength, new Csla.Validation.CommonRules.MaxLengthRuleArgs("SystemGenNo", 30));
            ValidationRules.AddRule(CommonRules.StringRequired, "AccountsReceivableNo");
            ValidationRules.AddRule(CommonRules.StringMaxLength, new Csla.Validation.CommonRules.MaxLengthRuleArgs("AccountsReceivableNo", 30));
            ValidationRules.AddRule(CommonRules.StringRequired, "CustomerNo");
            ValidationRules.AddRule(CommonRules.StringMaxLength, new Csla.Validation.CommonRules.MaxLengthRuleArgs("CustomerNo", 20));
            ValidationRules.AddRule(CommonRules.StringRequired, "CustomerName");
            ValidationRules.AddRule(CommonRules.StringMaxLength, new Csla.Validation.CommonRules.MaxLengthRuleArgs("CustomerName", 200));
            ValidationRules.AddRule(CommonRules.StringRequired, "Remark");
            ValidationRules.AddRule(CommonRules.StringMaxLength, new Csla.Validation.CommonRules.MaxLengthRuleArgs("Remark", 500));
            ValidationRules.AddRule(CommonRules.StringRequired, "Contact");
            ValidationRules.AddRule(CommonRules.StringMaxLength, new Csla.Validation.CommonRules.MaxLengthRuleArgs("Contact", 20));
            ValidationRules.AddRule(CommonRules.StringRequired, "Tel");
            ValidationRules.AddRule(CommonRules.StringMaxLength, new Csla.Validation.CommonRules.MaxLengthRuleArgs("Tel", 15));
            ValidationRules.AddRule(CommonRules.StringRequired, "Mobile");
            ValidationRules.AddRule(CommonRules.StringMaxLength, new Csla.Validation.CommonRules.MaxLengthRuleArgs("Mobile", 15));
            ValidationRules.AddRule(CommonRules.StringRequired, "Address");
            ValidationRules.AddRule(CommonRules.StringMaxLength, new Csla.Validation.CommonRules.MaxLengthRuleArgs("Address", 200));
            ValidationRules.AddRule(CommonRules.StringRequired, "MicroMsg");
            ValidationRules.AddRule(CommonRules.StringMaxLength, new Csla.Validation.CommonRules.MaxLengthRuleArgs("MicroMsg", 100));
            ValidationRules.AddRule(CommonRules.StringRequired, "QQ");
            ValidationRules.AddRule(CommonRules.StringMaxLength, new Csla.Validation.CommonRules.MaxLengthRuleArgs("QQ", 100));
            ValidationRules.AddRule(CommonRules.StringRequired, "AddUserName");
            ValidationRules.AddRule(CommonRules.StringMaxLength, new Csla.Validation.CommonRules.MaxLengthRuleArgs("AddUserName", 30));
            //base.AddBusinessRules();
        }
        #endregion

        #region Factory Methods 工厂方法

        public V_BH_AccountsReceivable() { }

        public static V_BH_AccountsReceivable New()
        {
            return DataPortal.Create<V_BH_AccountsReceivable>();
        }
        public static V_BH_AccountsReceivable Get(Guid ID)
        {
            return DataPortal.Fetch<V_BH_AccountsReceivable>(new SingleLazyCriteria<V_BH_AccountsReceivable, Guid>(ID, false));
        }
        public static void Delete(Guid ID)
        {
            DataPortal.Delete(ID);
        }

        #endregion

        #region Data Access 数据访问
        //由于DataPortal_create中没有对数据库的访问，所有标记了Runlocal特性，使得该方法只需在本地运行即可
        //如果代码中有访问数据库的方法，那么就不要用这个特性，使数据门户可以根据情况将此方法的运行选择在应用服务器还是本地
        [RunLocal()]
        protected override void DataPortal_Create()
        {
            //在此调用CheckRules对所有的属性进行验证规则的检查，这样检查只运行一次。
            ValidationRules.CheckRules();
        }


        //可以看到此方法没有标记任何特性
        //此方法要访问数据库所以不能标记本地，由于她没有任何数据库更新操作所以也无需事务性的保护
        //此方法调用结束后会隐含的调用MarkOld（）方法
        private void DataPortal_Fetch(Guid ID)
        {
            //通过连接字符串建立SQL连接对象，Using符号代表，范围释放的意思，SqlConnection对象会在此范围后被释放掉
            using (var ctx = ConnectionManager<SqlConnection>.GetManager(ConnectionStringNames))
            {
                IDb db = DbFactory.Instance.GetDb(ctx.Connection);
                IQuery q = db.Query();
                q.Constrain("ID").Equal(ID);

                var data = db.Select<V_BH_AccountsReceivable>(q)[0];
                LoadProperty(IDProperty, data.ID);
                LoadProperty(SystemGenNoProperty, data.SystemGenNo);
                LoadProperty(AccountsReceivableNoProperty, data.AccountsReceivableNo);
                LoadProperty(SummaryDateProperty, data.SummaryDate);
                LoadProperty(SumStartDateProperty, data.SumStartDate);
                LoadProperty(SumEndDateProperty, data.SumEndDate);
                LoadProperty(CustomerNoProperty, data.CustomerNo);
                LoadProperty(CustomerNameProperty, data.CustomerName);
                LoadProperty(AmountProperty, data.Amount);
                LoadProperty(RebateProperty, data.Rebate);
                LoadProperty(RebateAmountProperty, data.RebateAmount);
                LoadProperty(FactAmountProperty, data.FactAmount);
                LoadProperty(ReceivedAmountProperty, data.ReceivedAmount);
                LoadProperty(RemarkProperty, data.Remark);
                LoadProperty(InitFlagProperty, data.InitFlag);
                LoadProperty(ContactProperty, data.Contact);
                LoadProperty(TelProperty, data.Tel);
                LoadProperty(MobileProperty, data.Mobile);
                LoadProperty(AddressProperty, data.Address);
                LoadProperty(MicroMsgProperty, data.MicroMsg);
                LoadProperty(QQProperty, data.QQ);
                LoadProperty(CreditProperty, data.Credit);
                LoadProperty(CreditPeriodProperty, data.CreditPeriod);
                LoadProperty(AddUserProperty, data.AddUser);
                LoadProperty(AddUserNameProperty, data.AddUserName);
                LoadProperty(AddTimeProperty, data.AddTime);
            }
        }

        [Transactional(TransactionalTypes.TransactionScope)]
        protected override void DataPortal_DeleteSelf()
        {
            //依然是调用下面的方法删除的，只是他此时可以获得自己的Id
            DataPortal_Delete(IDProperty);
        }
        #endregion
    }
}

